package com.qfedu.mtl.dao.impl;

import com.qfedu.mtl.dao.BrandDao;
import com.qfedu.mtl.entity.Brand;
import com.qfedu.mtl.query.BrandQuery;
import com.qfedu.mtl.util.DBUtil;
import com.qfedu.mtl.util.StrUtil;
import com.qfedu.mtl.util.handler.impl.BeanHandler;
import com.qfedu.mtl.util.handler.impl.BeanListHandler;
import com.qfedu.mtl.util.handler.impl.NumberHandler;

import java.util.ArrayList;
import java.util.List;

public class BrandDaoImpl implements BrandDao {
    @Override
    public List<Brand> selectData(Integer page, Integer size, BrandQuery query) {
        List<Object> params = new ArrayList<>();
        StringBuilder sqlBuilder = new StringBuilder("select * from tb_brand where 1=1 ");
        if(!StrUtil.isNullOrEmpty( query.getBrandName() )){
            sqlBuilder.append(" and  brand_name = ? ");
            params.add( query.getBrandName()  );
        }
        if( query.getBrandStatus()!=null ){
            sqlBuilder.append(" and  brand_status = ? ");
            params.add( query.getBrandStatus() );
        }
        sqlBuilder.append(" limit ?,?");
        params.add(  (page-1)*size  );
        params.add( size );
        return DBUtil.executeDQL(sqlBuilder.toString(), DBUtil.getConn(),new BeanListHandler<>(Brand.class,true), params.toArray() );
    }

    @Override
    public Integer selectTotal(BrandQuery query) {
        List<Object> params = new ArrayList<>();
        StringBuilder sqlBuilder = new StringBuilder("select count(*) from tb_brand where 1=1 ");
        if(!StrUtil.isNullOrEmpty( query.getBrandName() )){
            sqlBuilder.append(" and  brand_name = ? ");
            params.add( query.getBrandName()  );
        }
        if( query.getBrandStatus()!=null ){
            sqlBuilder.append(" and  brand_status = ? ");
            params.add( query.getBrandStatus() );
        }
        Long aLong = DBUtil.executeDQL(sqlBuilder.toString(), new NumberHandler<>(Long.class), params.toArray());
        return aLong.intValue();
    }

    @Override
    public Brand selectById(Integer id) {
        String sql = "select * from tb_brand where brand_id=?";
        return DBUtil.executeDQL(sql,DBUtil.getConn(),new BeanHandler<>(Brand.class,true),id);
    }

    @Override
    public int insert(Brand obj) {
        String sql = "insert into tb_brand(brand_name, brand_logo ,  brand_desc, create_time,   brand_status) values(?,?,?,?,?)";
        return DBUtil.executeDML(sql,DBUtil.getConn(),
                obj.getBrandName(),obj.getBrandLogo(),obj.getBrandDesc(),obj.getCreateTime(),obj.getBrandStatus());
    }




    @Override
    public int delete(Integer id) {
        String sql =  "delete from tb_brand where brand_id=?";
        return DBUtil.executeDML(sql,DBUtil.getConn(),id);
    }

    @Override
    public int update(Brand obj) {
        String sql = "update tb_brand set   brand_name=?, brand_logo=? ,  brand_desc=?, brand_status=?  where brand_id=?";
        return DBUtil.executeDML(sql,DBUtil.getConn(),
                obj.getBrandName(),obj.getBrandLogo(),obj.getBrandDesc(),obj.getBrandStatus(),obj.getBrandId() );
    }

    @Override
    public int deleteBatch(List<Integer> ids) {
        String sql = "delete from tb_brand where brand_id in (";
        for (int i = 0; i < ids.size(); i++) {
            if( i!= ids.size()-1){
                 sql += " ?, ";
            }else{
                 sql += "?)";
            }
        }
        return DBUtil.executeDML(sql,DBUtil.getConn(),ids.toArray());
    }
}
